package util;

import java.io.File;
import java.io.UnsupportedEncodingException;

import ui.FileUploadUI;
import upload.SendToHost;


public class Utility
{

	public Utility()
	{
	}
	
	public static boolean isEmpty(String str){
		if(str == null)
			return true;
		if(str.trim().equals(""))
			return true;
		
		return false;
	}
	
	public static String toHexString(int[] b,int len){
		String str="";
		int ch;
		for(int i=0;i<len;i++){
			ch = b[i];
			String hexValue = Integer.toHexString(ch);
			if (hexValue.length() == 1){
				hexValue = "0" + hexValue;
			}
			str += hexValue;
		}
		return str;
	}
	
	public static byte[] toBinArray(String hexStr){
		int ch,ch2,ch3;
		byte[] b = new byte[hexStr.length()/2];
		for(int i=0,len=hexStr.length();i<len;i+=2){
			ch = hexStr.charAt(i);
			if (Character.isLetter(ch) || Character.isDigit(ch)) {
				ch2 = hexStr.charAt(i+1);
				ch3 = Integer.parseInt((char) ch + "" + (char) ch2, 16);
				b[i/2]=(byte)ch3;
			}
		}
		return b;
	}
	
	public static void send(String urlStr, byte[] content, int len){
		if(urlStr == null)
			return;
		
		while (true) {
			try{
				SendToHost sth = new SendToHost(FileUploadUI.host,urlStr);
				sth.setContent(content, len);
				String res = sth.send();
				if("ok".equals(res)||"error".equals(res))break;
				else {
					try {
						Thread.sleep(10000);
					} catch (Exception e) {
				}
			}
			}catch(Exception ex){}
		}
	}
	
	public static void send(String urlStr, String content){
		if(urlStr == null)
			return;
		
		while (true) {
			try{
				SendToHost sth = new SendToHost(FileUploadUI.host,urlStr);
				sth.setContent(content);
				String res = sth.send();
				if("ok".equals(res)||"error".equals(res))break;
				else {
					try {
						Thread.sleep(10000);
					} catch (Exception e) {
				}
			}
			}catch(Exception ex){}
		}
	}
	
	public static void send(String urlStr){
		if(urlStr == null)
			return;
		
		while (true) {
			try{
				SendToHost sth = new SendToHost(FileUploadUI.host,urlStr);
				String res = sth.send();
				if("ok".equals(res)||"error".equals(res))break;
				else {
					try {
						Thread.sleep(10000);
					} catch (Exception e) {
				}
			}
			}catch(Exception ex){}
		}
	}
	
	public static String toUTF8String(String text){
		if(isEmpty(text))
			return "";
		
		try{
			return new String(text.getBytes("8859_1"), "UTF-8");
		}
		catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
			return "";
		}
	}
	
	public static String getFilePathWidthoutName(File file){
		if(file != null){
			String path = file.getPath();
			int index = -1;
			for(int i=path.length()-1;i>=0;i--){
				char ch = path.charAt(i);
				if(ch == '/' || ch == '\\'){
					index = i;
					break;
				}
			}
			if(index != -1){
				return path.substring(0, index);
			}
		}
		
		return "c:/";
	}

	public static void sleep(int nSecond)
	{
		try
		{
			Thread.sleep(nSecond);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public static void log(String sMsg)
	{
		System.err.println(sMsg);
	}

	public static void log(int sMsg)
	{
		System.err.println(sMsg);
	}
	
	public static String URLEncode(String s)
	{
		try{
			return java.net.URLEncoder.encode(s,"UTF-8").replaceAll("\\+","%20");
		}
		catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
			return s;
		}
	}
}
